'use strict';
/**
 * Created by FreezeSoul on 2015/10/23.
 */
define([
    "app/core/base",
    "jquery",
    "echarts",
    'loglevel',
], function (Chart, $, ec, log) {

    //TODO:自定义组件属性
    var property = {
        //背景色
        BackgroundColor: undefined,

        //序列设置{SeriesIndex:0,SeriesType:'bar',SeriesStack:'',LabelPosition:''}
        SeriesOption: undefined,


        //需要针对组件映射属性至组件Option
        MapOption: function (option) {
            var _this = this;

            //背景色设置
            if (this.BackgroundColor) {
                option.backgroundColor = this.BackgroundColor;
            }

            //序列类型与堆积
            if (this.SeriesOption) {
                $.each(this.SeriesOption, function (i, seriesoption) {
                    var series = _.findWhere(option.series, {name: seriesoption.SeriesName});
                    if (series) {
                        if (seriesoption.LabelPosition) {
                            if (!series.itemStyle) series.itemStyle = {};
                            if (!series.itemStyle.normal) series.itemStyle.normal = {};
                            if (!series.itemStyle.normal.label) series.itemStyle.normal.label = {};

                            series.itemStyle.normal.label.show = true;
                            series.itemStyle.normal.label.position = seriesoption.LabelPosition;
                        }
                    }
                });
            }
        }
    };

    var gauge = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._chart = null;
            this._option = {
                Binding: [],
                Events: [],
                Extend: $.extend({}, property),
                Option: {
                    tooltip: {
                        formatter: "{a} <br/>{b} : {c}%"
                    },
                    series: [
                        {
                            name: '业务指标',
                            type: 'gauge',
                            detail: {formatter: '{value}%'},
                            data: [{value: 50, name: '完成率'}]
                        }
                    ]
                }
            };

        },
        init: function (element) {
            try {
                this._chart = ec.init(element, null, {
                    renderer: 'canvas'
                });
                this._bindEvent();
            } catch (e) {
                log.error(e);
            }
        },
        _bindEvent: function () {
            var _this = this;
            this._chart.on("click", function (param) {
                if (_this._layout && _this._layout.navigate && _this._option.Events) {
                    var registerEvent = _.findWhere(_this._option.Events, {Event: "SeriesClick"});//todo 需要根据map修改
                    if (registerEvent) {
                        var pageId = registerEvent.PageId;
                        var params = registerEvent.Params;
                        var args = [];
                        var dParam = _.findWhere(params, {Value: "Dimension"});
                        if (dParam && dParam.ParamName) {
                            args.push({
                                ParamName: dParam.ParamName,
                                ParamValue: param.name
                            });
                        }
                        var mParam = _.findWhere(params, {Value: "Measure"});
                        if (mParam && mParam.ParamName) {
                            args.push({
                                ParamName: mParam.ParamName,
                                ParamValue: param.data
                            });
                        }
                        var sParam = _.findWhere(params, {Value: "SeriesName"});
                        if (sParam && sParam.ParamName) {
                            args.push({
                                ParamName: sParam.ParamName,
                                ParamValue: param.seriesName
                            });
                        }
                        _this._layout.navigate(pageId, args);
                    }
                    param.event.event.preventDefault();
                }
            });
        },
        example: function () {
            try {
                this._chart.setOption(this._option.Option, true);
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                var propertyCopy = $.extend(true, {}, property);
                $.extend(true, propertyCopy, this._option.Extend);
                propertyCopy.MapOption(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        //TODO:需要重写的部分  复制过来的 还没修改那
        _bindData: function (data) {
            var tables = data;
            var _this = this;

            //绑定数据
            $.each(this._option.Extend.SeriesOption, function (i, series) {
                if (series.DataSetCode && series.DataValue) {
                    var table = tables[series.DataSetCode];

                    var binding = _.findWhere(_this._option.Binding, {DataSetCode: series.DataSetCode});

                    var dataValue = undefined;
                    if (series.DataValue) {
                        dataValue = _.findWhere(binding.Measures, {Code: series.DataValue}).Column;
                    }
                    var dataValues = [];
                    $.each(table, function (j, row) {
                        if (dataValue != undefined && row[dataValue] != '') {
                            dataValues.push({value: row[dataValue], name: series.DataName});
                        }
                    });
                    if (_this._option.Option.series.length >= i + 1) {
                        _this._option.Option.series[i].name = series.SeriesName;
                        _this._option.Option.series[i].data = dataValues;
                    } else {
                        _this._option.Option.series.push(
                            {
                                name: binding.Name,
                                type: 'gauge',
                                detail: {formatter: '{value}%'},
                                data: dataValues
                            })
                    }
                }
            });
            //移除手工设置的
            if (this._option.Option.series.length > this._option.Extend.SeriesOption.length) {
                this._option.Option.series.splice(this._option.Extend.SeriesOption.length, this._option.Option.series.length - this._option.Extend.SeriesOption.length);
            }
        },
        setData: function (data) {
            try {
                var _this = this;
                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                    && this._option.Extend.SeriesOption.length > 0
                    && _.every(this._option.Extend.SeriesOption, function (series) {
                        return series.DataSetCode && series.DataValue;
                    })) {
                    this._readProperty();

                    this._bindData(data);

                    this._readProperty();
                }

                _this._chart.setOption(_this._option.Option, true);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
                this._chart.setTheme(theme);
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
                this._chart.resize();
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {
                this._chart.dispose();
            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return null;
        },
        getChart: function () {
            return this._chart;
        }
    });

    return gauge;
});